package tenumn.energytech.registrys.pipeline;

import com.google.common.collect.Maps;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraftforge.energy.CapabilityEnergy;
import org.jetbrains.annotations.Nullable;
import tenumn.energytech.registrys.CapabilitiesRegistry;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Map;

public class HeartPipeBlock extends Block implements EntityBlock {
    public static final Map<Direction, BooleanProperty> PROPERTY_MAP;

    static {
        Map<Direction, BooleanProperty> map = Maps.newEnumMap(Direction.class);
        map.put(Direction.NORTH, BlockStateProperties.NORTH);
        map.put(Direction.EAST, BlockStateProperties.EAST);
        map.put(Direction.SOUTH, BlockStateProperties.SOUTH);
        map.put(Direction.WEST, BlockStateProperties.WEST);
        map.put(Direction.UP, BlockStateProperties.UP);
        map.put(Direction.DOWN, BlockStateProperties.DOWN);
        PROPERTY_MAP = Collections.unmodifiableMap(map);
    }

    public HeartPipeBlock(Properties properties) {
        super(properties);
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
        pBuilder.add(PROPERTY_MAP.values().toArray(new Property[0]));
        super.createBlockStateDefinition(pBuilder);
    }

    @Nullable
    @Override
    public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
        return WireRegistry.heartPipeBlockEntity.create(pPos, pState);
    }

    @Override
    public BlockState getStateForPlacement(@Nonnull BlockPlaceContext context) {
        BlockState state = this.defaultBlockState();
        for (Direction facing : Direction.values()) {
            Level world = context.getLevel();
            BlockPos facingPos = context.getClickedPos().relative(facing);
            BlockState facingState = world.getBlockState(facingPos);
            state = state.setValue(PROPERTY_MAP.get(facing), this.canConnect(world, facing.getOpposite(), facingPos, facingState));
        }
        return state;
    }

    @Override
    public BlockState updateShape(BlockState pState, Direction pDirection, BlockState pNeighborState, LevelAccessor pLevel, BlockPos pCurrentPos, BlockPos pNeighborPos) {
        return pState.setValue(PROPERTY_MAP.get(pDirection), this.canConnect(pLevel, pDirection.getOpposite(), pNeighborPos, pNeighborState));
    }

    @Override
    @SuppressWarnings("deprecation")
    public void neighborChanged(@Nonnull BlockState state, @Nonnull Level world, @Nonnull BlockPos pos, @Nonnull Block fromBlock, @Nonnull BlockPos fromPos, boolean isMoving) {
        if (!world.isClientSide) {
            BlockEntity tileEntity = world.getBlockEntity(pos);
            if (tileEntity instanceof WireBlockEntity) {
                //启用连接的机器
                SimpleHeartNetwork.Factory.get(world).enableBlock(pos, tileEntity::setChanged);
            }
        }
    }

    private boolean canConnect(@Nonnull LevelAccessor world, @Nonnull Direction facing, @Nonnull BlockPos pos, @Nonnull BlockState state) {//判断是否能连接，是管道则为true
        if (!state.getBlock().equals(WireRegistry.heartPipeBlock.get())) {
            BlockEntity tileEntity = world.getBlockEntity(pos);
            return tileEntity != null && tileEntity.getCapability(CapabilitiesRegistry.HEART_CAPABILITY, facing).isPresent();
        }
        return true;
    }
}
